package com.example.test.files;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * @Author: fxy
 * @Date: 2022/01/12/10:32
 * @Description:
 */
public class Demo1 {
    public static void main(String[] args) throws Exception {
        /*Path path = Paths.get("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\dic1");
        Path d = Files.createDirectories(path);*/
        //fileCopy();
        fileMove();
    }

    /**
     * 重命名  移动
     * @throws Exception
     */
    private static void fileMove() throws Exception {
        Path sourcePath = Paths.get("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\01.txt");
        Path destinationPath = Paths.get("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\01.txt");

        //Files.copy(sourcePath,destinationPath);
        Files.move(sourcePath,destinationPath, StandardCopyOption.REPLACE_EXISTING);
    }

    private static void fileCopy() throws Exception {
        Path sourcePath = Paths.get("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\01.txt");
        Path destinationPath = Paths.get("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\dic1\\01.txt");

        //Files.copy(sourcePath,destinationPath);
        Files.copy(sourcePath,destinationPath, StandardCopyOption.REPLACE_EXISTING);
    }

    //直接缓存区
    @Test
    public void b03() throws Exception {
        String infile = "D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\01.txt";
        FileInputStream fin = new FileInputStream(infile);
        BufferedReader rin = new BufferedReader(new InputStreamReader(fin));

        String outfile = "D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\02.txt";
        FileOutputStream fout = new FileOutputStream(outfile);
        BufferedWriter wou = new BufferedWriter(new OutputStreamWriter(fout));
        while(true){

            String b = rin.readLine();
            if(b!=null&&!b.startsWith("INSERT")){
                continue;
            }
            wou.write(b==null?"":b);
        }
    }
}
